package com.edu.cdp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.cdp.DTO.AdminDTO;
import com.edu.cdp.service.CommonService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.cdp.domain.Admin;
import com.edu.cdp.service.AdminService;
import com.edu.cdp.mapper.AdminMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
* @author Yang Can
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2024-05-29 18:27:22
*/
@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CommonService commonService;

    @Override
    public Admin login(String username, String password) {
        QueryWrapper<Admin> query = new QueryWrapper<Admin>();
        query.eq("name", username);
        query.eq("password", password);

        Admin admin = adminMapper.selectOne(query);

        if (admin!= null) {
            return admin;
        } else {
            return null;
        }
    }

    @Override
    public Admin newUpdate(AdminDTO admin) {
        Admin oldAdmin = adminMapper.selectById(admin.getId());

        if (oldAdmin != null) {
            if (admin.getName() != null && admin.getName() != ""){
                oldAdmin.setName(admin.getName());
            }

            if (admin.getPassword() != null && admin.getPassword() != ""){
                oldAdmin.setSex(admin.getSex());
            }

            if (admin.getPhone() != null && admin.getPhone() != ""){
                oldAdmin.setPhone(admin.getPhone());
            }

            return oldAdmin;
        } else {
            log.error("数据库中不存在该用户");
            return null;
        }
    }

    @Override
    @Transactional
    public Admin updateAvatar(Integer id, MultipartFile avatar) {
        String avatarPath = commonService.upload(avatar);

        if (avatarPath == null || avatarPath.isEmpty()) {
            log.error("头像上传失败");
        }

        Admin adminToUpdate = new Admin();
        adminToUpdate.setId(id);
        adminToUpdate.setAvatar(avatarPath);

        int result = adminMapper.update(adminToUpdate,
                Wrappers.<Admin>lambdaUpdate().eq(Admin::getId, id));

        if (result > 0) {
            return adminMapper.selectById(id);
        } else {
            return null;
        }
    }

    @Override
    public Admin add(AdminDTO admin) {
        Admin newAdmin = new Admin();
        BeanUtils.copyProperties(admin, newAdmin);

        int result = adminMapper.insert(newAdmin);

        if (result > 0) {
            return newAdmin;
        } else {
            log.error("添加失败");
            return null;
        }
    }
}




